home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / MouseInfo / UMouseTrackBehavior.cp < prev    next >
Encoding:
Text File  |  1995-02-04  |  5.8 KB  |  229 lines  |  [TEXT/MPS ]

  1. //     UMouseTrackBehavior.cp 
  2. //     Copyright © 1992 by Apple Computer, Inc. All rights reserved.
  3. //    Kent Sandvik DTS
  4. //    This file contains the basic TMouseTrackBehavior member functions
  5. //    Version Info (latest first):
  6. //
  7. //    <1>        khs        1.0        First final version
  8. //    <2>        khs        1.0.1    Fixed a memory leak in TMapApplication::GetSleepValue()
  9.  
  10.  
  11. #ifndef __MOUSETRACKBEHAVIOR__
  12. #include "UMouseTrackBehavior.h"
  13. #endif
  14.  
  15.  
  16. //    Initialize needed parts for the MouseTrackBehavior modules
  17. #pragma segment AInit
  18.      void InitMouseTrackBehavior()
  19. {
  20.  
  21.     macroDontDeadStrip(TMouseTrackBehavior);
  22.     macroDontDeadStrip(TTrackWindow);
  23.  
  24.     RegisterStdType("TTrackWindow", 'ttrk');
  25. }
  26.  
  27.  
  28. //    Close the remove the single swallow application when closing the floating window
  29.  
  30. #undef Inherited
  31. #define Inherited TFloatWindow
  32.  
  33. #pragma segment AClose
  34. DefineClass(TTrackWindow, Inherited);
  35.  
  36.      void TTrackWindow::Close()
  37. {
  38.     gMouseTrackWindow = NULL;                    // signal that it's OK to open one again
  39.  
  40.     gApplication->RemoveBehavior(fBehavior);    //    Get rid of the TSwallowBehavior in gApplication...
  41.  
  42.     Inherited::Close();
  43. }
  44.  
  45.  
  46. //    Get the pointer to the single swallow behavior which we need when removing it    
  47. #pragma segment ARes
  48.      void TTrackWindow::GetOriginatorBehavior(TSwallowBehavior* behavior)
  49. {
  50.     fBehavior = behavior;
  51. }
  52.  
  53.  
  54. #undef Inherited
  55. #define Inherited TBehavior
  56.  
  57. #pragma segment AInit
  58. DefineClass(TMouseTrackBehavior, Inherited);
  59.  
  60. //     Create behavior and define default values
  61.      void TMouseTrackBehavior::IMouseTrackBehavior(ResNumber menuID)
  62. {
  63.     this->IBehavior(kMouseTrackBehavior);
  64.     this->SetIdleFreq(0);                        // call as often as possible
  65.     this->SetEnabled(TRUE);                        // enable behavior    
  66.     fMenuID = menuID;
  67. }
  68.  
  69.  
  70. //    Initialized any static fields in the behavior
  71. #pragma segment AInit
  72.      TMouseTrackBehavior::TMouseTrackBehavior()
  73. {
  74.     fMenuID = 0;
  75. }
  76.  
  77.  
  78. //    Setup needed menus dynamically, i.e. mouse tracking menu if floating window not present
  79. #pragma segment ARes
  80.      void TMouseTrackBehavior::DoSetupMenus()
  81. {
  82.     Inherited::DoSetupMenus();
  83.     Enable(cOpenTracker, gMouseTrackWindow == NULL);
  84. }
  85.  
  86.  
  87. //    Do menu commands associated with the mousetrack behavior, in this case only one
  88. //    menu, open floating window and install the event swallowing behavior
  89. #pragma segment ASelCommand
  90.      void TMouseTrackBehavior::DoMenuCommand(CommandNumber theNum)
  91. {
  92.     if (theNum == cOpenTracker && gMouseTrackWindow == NULL)
  93.     {
  94.         this->CreateWindoid();                    // create windoid on the flight
  95.         this->CreateSwallowBehavior();            // create and install event behavior
  96.         gMouseTrackWindow->Open();                // open windoid on screen
  97.         Enable(cOpenTracker, FALSE);            // disable the menu entry
  98.     }
  99.     else
  100.         Inherited::DoMenuCommand(theNum);
  101. }
  102.  
  103.  
  104. //    Create windoid, only one which is used by all open documents
  105. #pragma segment AInit
  106.      void TMouseTrackBehavior::CreateWindoid()
  107. {
  108.     FailInfo fi;
  109.     TTrackWindow * aWindow = NULL;
  110.  
  111.     Try(fi)
  112.     {
  113.         //    Create Windoid
  114.         aWindow = (TTrackWindow *)gViewServer->NewTemplateWindow(kFloatingWindow, NULL);
  115.         FailNIL(aWindow);
  116.         gMouseTrackWindow = aWindow;            // register the windoid globally
  117.  
  118.         fi.Success();
  119.     }
  120.     else
  121.         fi.ReSignal();
  122. }
  123.  
  124.  
  125. //    Create swallow behavior which will eat all events, also install it to the main
  126. //    gApplication framework and let the floating window let know about this behavior
  127. #pragma segment AInit
  128.      void TMouseTrackBehavior::CreateSwallowBehavior()
  129. {
  130.     //    Install swallow behavior which takes *every* event!
  131.     TSwallowBehavior * aSwallowBehavior = new TSwallowBehavior;
  132.     aSwallowBehavior->ISwallowBehavior();
  133.     gApplication->AddBehavior(aSwallowBehavior);
  134.     gMouseTrackWindow->GetOriginatorBehavior(aSwallowBehavior);// windoid needs to know the current behavior    
  135. }
  136.  
  137. #undef Inherited
  138. #define Inherited TBehavior
  139.  
  140. #pragma segment AInit
  141. DefineClass(TSwallowBehavior, Inherited);
  142.  
  143. //    Initialize the swallow behavior, it should find out the resources to the floating
  144. //    window (fields) which it needs when it updates them
  145.      void TSwallowBehavior::ISwallowBehavior()
  146. {
  147.     this->IBehavior(kNoIdentifier);
  148.     this->SetIdleFreq(0);                        // call as often as possible
  149.  
  150.     fTrackWindow = gMouseTrackWindow;            // get the global/only Windoid address
  151.  
  152.     //    Get Resource pointers
  153.     fMouseStatus = (TStaticText *)fTrackWindow->FindSubView('mous');
  154.     fVertical = (TNumberText *)fTrackWindow->FindSubView('numv');
  155.     fHorizontal = (TNumberText *)fTrackWindow->FindSubView('numh');
  156. }
  157.  
  158.  
  159. //    Get access to all events, and trigger actions based on key events, in our
  160. //    case mouseUp and mouseDowns
  161. #pragma segment ARes
  162.      Boolean TSwallowBehavior::DoToolboxEvent(TToolboxEvent* event)
  163. {
  164.     WindowPtr aWMgrWindow;
  165.  
  166.     short whereMouseDown = FindWindow(event->fEventRecord.where, &aWMgrWindow);
  167.     TWindow * aWindow = gApplication->WMgrToWindow(aWMgrWindow);
  168.  
  169.     if (event && (event->fIdentifier == mouseDown))
  170.         fEventType = mouseDown;
  171.  
  172.     else if (event && (event->fIdentifier == mouseUp))
  173.         fEventType = mouseUp;
  174.  
  175.     else if (event && (event->fIdentifier == activateEvt))
  176.         fEventType = mouseUp;
  177.  
  178.     if (aWindow)
  179.     {
  180.         VPoint theMouse(event->fEventRecord.where);
  181.         aWindow->SuperToLocal(theMouse);
  182.         fMouseVPoint = theMouse;
  183.     }
  184.  
  185.     VRect dummy;
  186.     if (gMouseTrackWindow != NULL)
  187.         this->Draw(dummy);                        // only draw if windoid exists
  188.  
  189.     return Inherited::DoToolboxEvent(event);
  190. }
  191.  
  192.  
  193. //    Draw contents inside floating window
  194. #pragma segment ASelCommand
  195.      void TSwallowBehavior::Draw(const VRect& area)
  196. {
  197.     if (gMouseTrackWindow != NULL)
  198.     {
  199.         //    Draw Mouse status
  200.         if (fEventType == mouseDown)
  201.         {
  202.             fMouseStatus->SetText("MouseDown", kRedraw);
  203.             fTrackWindow->Update();
  204.         }
  205.         else if (fEventType == mouseUp)
  206.         {
  207.             fMouseStatus->SetText("MouseUp", kRedraw);
  208.             fTrackWindow->Update();
  209.         }
  210.  
  211.         //    Draw coordinates
  212.         //    Avoid de-referencing problems by using stack based variables for NumToString
  213.         CStr255 tempH,  tempV;
  214.         VPoint tempVP;
  215.  
  216.         tempVP = fMouseVPoint;
  217.  
  218.         NumToString(tempVP.h, tempH);
  219.         NumToString(tempVP.v, tempV);
  220.         fHorizontal->SetText(tempH, kRedraw);
  221.         fVertical->SetText(tempV, kRedraw);
  222.         fTrackWindow->Update();
  223.     }
  224.  
  225.     Inherited::Draw(area);
  226. }
  227.  
  228.  
  229.